package com.yuerlive.rainbow.module.mine;

import android.content.ClipboardManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Space;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.protobuf.Api;
import com.yuerlive.rainbow.API;
import com.yuerlive.rainbow.BuildConfig;
import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseActivity;
import com.yuerlive.rainbow.common.NetBroadcastReceiver;
import com.yuerlive.rainbow.module.login.LoginActivity;
import com.yuerlive.rainbow.module.login.LoginWayActivity;
import com.yuerlive.rainbow.nim.NimHelper;
import com.yuerlive.rainbow.utils.GlideCatchUtil;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.utils.PreferenceUtils;
import com.yuerlive.rainbow.utils.UserProxy;
import com.yuerlive.rainbow.widget.CircleImageView;
import com.yuerlive.rainbow.widget.CustomDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Admin on 2017/5/21.
 */

public class MySettingActivity extends BaseActivity {
    @BindView(R.id.rlClearCache)
    RelativeLayout rlClearCache;
    @BindView(R.id.tvCache)
    TextView tvCache;
    @BindView(R.id.rlCustomer)
    RelativeLayout rlCustomer;
    @BindView(R.id.rlAboutOur)
    RelativeLayout rlAboutOur;
    @BindView(R.id.rlLogintOut)
    RelativeLayout rlLogintOut;
    @BindView(R.id.root)
    View root;

    @BindView(R.id.rlHost)
    RelativeLayout rlHost;

    @BindView(R.id.spinner)
    Spinner spinner;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting_layout;
    }

    @Override
    protected void initViews() {
        setTitle("设置");
        tvCache.setText(GlideCatchUtil.getInstance().getCacheSize());
        if (BuildConfig.DEBUG) {
            rlHost.setVisibility(View.VISIBLE);
            String[] hosts = getResources().getStringArray(R.array.host_config);
            spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hosts));

            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    setHost(hosts[position]);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        } else {
            rlHost.setVisibility(View.GONE);
        }

    }

    private void setHost(String host) {
        showSnackbar(getRootView(mActivity), "设置成功，请重启手机", 0);
        PreferenceUtils.putString("host", host);
    }

    @OnClick({R.id.rlClearCache, R.id.rlLogintOut, R.id.rlAboutOur, R.id.tvCustomer})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlClearCache:
                createCleanCacheDialog();
                break;
            case R.id.rlLogintOut:
                rlLogintOut.setVisibility(View.GONE);
                UserProxy.setUser(null);
                finish();
                NimHelper.logout();
                jumpToActivity(LoginWayActivity.class);
                NetBroadcastReceiver.removeAllListener();
                break;
            case R.id.rlAboutOur:
                jumpToActivity(AboutActivity.class);
                break;
            case R.id.tvCustomer:
                ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                clipboardManager.setText(((TextView) view).getText().toString());
                showSnackbar(root, "客服QQ群已复制到剪贴板", 0);
                break;

        }
    }

    private void createCleanCacheDialog() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.layout_choose_dialog, null);
        final CustomDialog dialog = new CustomDialog(this, contentView);
        TextView btnCancel = (TextView) contentView.findViewById(R.id.btnCancel);
        TextView btnSure = (TextView) contentView.findViewById(R.id.btnSure);
        TextView tvContent = (TextView) contentView.findViewById(R.id.tvContent);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 删除文件夹方法在主线程执行
                GlideCatchUtil.getInstance().cleanCatchDisk();
                GlideCatchUtil.getInstance().clearCacheMemory();
                GlideCatchUtil.getInstance().clearCacheDiskSelf();
                tvCache.setText(GlideCatchUtil.getInstance().getCacheSize());
                dialog.dismiss();
            }
        });
    }
}
